import {guid, isEmpty} from '@/utils/CommonUtils';
import {DefaultMenuButton} from '@/components/form/DefaultFormDefinition';

export default class {
  appId;

  createMenus(menuItem, appId) {
    if (isEmpty(menuItem)) return menuItem;
    this.appId = appId;
    if (menuItem.menuType === 'flow') {
      return this.createFlowMenus(menuItem);
    }
    if (menuItem.menuType === 'formMenu') {
      return this.createFormMenu(menuItem);
    }
    if (menuItem.menuType === 'link') {
      return this.createLinkMenu(menuItem);
    }
  }
  createLinkMenu(menuItem) {
    return {
      id: guid(),
      appId: this.appId,
      menuType: 'link',
      url: undefined,
      formMenuId: undefined,
      icon: undefined,
      actionTarget: 'current',
      fullScreen: '0',
      menuName: '自定义链接',
      flowId: undefined,
      component: 'CustomLinkView',
      parentId: undefined
    }
  }
  createFlowMenus(formMenuItem) {
    let id = guid();
    let res1 = {
      id,
      appId: this.appId,
      menuType: 'flow',
      formMenuId: formMenuItem.id,
      actionTarget: 'new',
      flowId: formMenuItem.flowId,
      fullScreen: '1',
      menuName: `${formMenuItem.menuName}(流程)`,
      path: `/plat/work/startProcess?action=add&uuid=${id}`,
      icon: formMenuItem.icon
    };

    id = guid();
    let res2 = {
      id,
      appId: this.appId,
      menuType: 'flowList',
      formMenuId: formMenuItem.id,
      actionTarget: 'current',
      flowId: formMenuItem.flowId,
      fullScreen: '0',
      path: `/${id}`,
      menuName: `${formMenuItem.menuName}(流程列表)`,
      component: 'FlowWork/List',
      icon: formMenuItem.icon
    };
    return [res1, res2];
  }

  createFormMenu(menuItem) {
    let temp = {
      id: guid(),
      menuType: 'formMenu',
      url: undefined,
      formMenuId: menuItem.id,
      actionTarget: 'current',
      fullScreen: '0',
      menuName: menuItem.menuName,
      parentId: undefined,
      sort: undefined,
      component: menuItem.listPageComponent,
      path: menuItem.listPageUrl,
      flowId: undefined
    };
    return [temp];
  }

  createDefaultFormMenuDefinition(menu, formDefinition) {
    menu.appId = this.appId;
    menu.id = guid();

    menu.listPageUrl = `/platwork/FormListView/` + menu.id;
    menu.listPageComponent = `platwork/FormListView`;

    menu.detailPageUrl = `/plat/work/detail`;
    menu.detailPageComponent = `components/platwork/DefaultFormDetailComponent`;
    menu.menuButtons = this.createFormMenuDefaultButtons(menu, formDefinition);
    return menu;
  }

  createFormMenuDefaultButtons(menu, formDefinition) {
    let res = [], button = undefined;
    button = JSON.parse(JSON.stringify(DefaultMenuButton));
    button.formMenuId = menu.id;
    button.id = guid();
    button.appId = this.appId;
    button.buttonName = '新建';
    button.buttonComponent = 'platwork/FormListButton/CreateFormButton'
    button.buttonActionType = 'createForm';
    this.copyViews(button, menu, formDefinition, 'edit');
    button.supportMultiple = true;
    res.push(button);


    button = JSON.parse(JSON.stringify(DefaultMenuButton));
    button.formMenuId = menu.id;
    button.appId = this.appId;
    button.id = guid();
    button.buttonName = '编辑';
    button.buttonComponent = 'platwork/FormListButton/EditFormButton'
    button.buttonActionType = 'edit';
    this.copyViews(button, menu, formDefinition, 'edit');
    button.supportMultiple = false;
    button.forRow = true;
    res.push(button);

    button = JSON.parse(JSON.stringify(DefaultMenuButton));
    button.formMenuId = menu.id;
    button.appId = this.appId;
    button.id = guid();
    button.buttonName = '详情';
    button.buttonActionType = 'detail';
    button.buttonComponent = 'platwork/FormListButton/DetailFormButton'
    button.forRow = true;
    this.copyViews(button, menu, formDefinition, 'show');
    button.supportMultiple = false;
    res.push(button);

    return res;
  }

  copyViews(button, menu, formDefinition, actionType) {
    button.pcViewId = menu.pcViewId;
    button.pcViewActionId = menu.pcViewActionId;
    button.mobileViewId = menu.mobileViewId;
    button.mobileViewActionId = menu.mobileViewActionId;

    try {
      let views = formDefinition.views;
      let view = views.find(v => v.main + '' === '1');
      let viewActions = formDefinition.viewActions;
      let viewAction = viewActions.find(v => v.viewId + '' === view.id && v.actionType === actionType);

      button.pcViewId = view.id;
      button.pcViewActionId = viewAction.id;
      button.mobileViewId = view.id;
      button.mobileViewActionId = viewAction.id;
    } catch (e) {
    }

    if (formDefinition.processForm) {
      button.pcViewId = undefined;
      button.pcViewActionId = undefined;
      button.mobileViewId = undefined;
      button.mobileViewActionId = undefined;
      try {
        button.processDefinitionId = formDefinition.flows[0].id;
      } catch (e) {
      }
    }
  }
}
